package com.gaofei.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author : gaofee
 * @date : 10:54 2021/1/26
 * @码云地址 : https://gitee.com/itgaofee
 * 消息确认的类
 */
@Component
public class MsgACK {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
//        rabbitTemplate.setChannelTransacted(true);//设置开启事务
        //消息确认的代码在这里写
        //生产者到交换机的消息确认的代码
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){

            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String s) {
                if(ack){
                    System.err.println("生产者--->交换机的消息确认成功");
                }else{
                    System.err.println("生产者--->交换机的消息发送不成功");
                }
            }
        });

        //交换机到队列的消息确认的代码
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback(){
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.err.println("交换机--->队列的消息的路由不成功我就会执行========");
            }
        });
    }
}
